package com.cicadamob.contact.api.group.asynctask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.cicadamob.contact.api.Utils.ContactConfig;
import com.cicadamob.contact.api.Utils.Logger.AppLogger;
import com.cicadamob.contact.api.Utils.NetWork.HttpRequestCallBack;
import com.cicadamob.contact.api.Utils.NetWork.HttpUtil;
import com.cicadamob.contact.api.Utils.NetWork.NetWorkUtil;
import com.cicadamob.contact.api.Utils.NetWork.HttpUtil.RequestMethod;
import com.cicadamob.contact.api.group.bean.Group;
import com.cicadamob.contact.api.group.bean.ResultModel;
import com.cicadamob.contact.api.group.dao.Group_Db_Dao;
import com.cicadamob.contact.api.group.http.Http_Group_Util;
import com.cicadamob.contact.api.group.http.JsonParseUtil;
import com.cicadamob.contact.api.service.Contact;
import com.cicadamob.contact.api.service.ServerUrl;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;

public class DelGroupRunnable implements Runnable {

	private Handler mHandler;
	private int what;
	private Context mContext;
	private boolean isOwer;
	private String groupID;
	private Group_Db_Dao dbHelper;
	public DelGroupRunnable(Handler mHandler,int what,Context mContext,String groupID,boolean isOwer){
		this.mHandler=mHandler;
		this.what=what;
		this.mContext=mContext;
		this.isOwer=isOwer;
		this.groupID=groupID;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		dbHelper=Group_Db_Dao.getInstance(mContext);
		ResultModel resultmodel = new ResultModel();
		if (!NetWorkUtil.isConnected(mContext)) {
			resultmodel.setRetcode(100);
			ArrayList<Group> sContacts = (ArrayList<Group>) dbHelper.queryAllGroup();
			resultmodel.setResultData(sContacts);
			sendMessage(resultmodel);
			return ;// 网络不异常
		}
//		try {
		
		mHandler.post(new  Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (isOwer) {
					
					String xmlData=
							Http_Group_Util.get_deletePersonalGroupXML(	groupID, "0");
					Map<String, String> requestMap=new HashMap<String, String>();
					requestMap.put(HttpUtil.XML, xmlData);
					 AppLogger.writeLogToFile(mContext, ContactConfig.DIRECTORY+"/httpGroupLog", "msg_group", "发送报文  url =" + ServerUrl.MOD_DELETE_PERSONAL_GROUP
					    		+"\r\n xml ="+xmlData, true);	
					HttpUtil.HttpRequest(mContext, RequestMethod.POST, ServerUrl.MOD_DELETE_PERSONAL_GROUP, false,requestMap, new HttpRequestCallBack<String>(){

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							// TODO Auto-generated method stub
							doResult(responseInfo.result);
							AppLogger.writeLogToFile(mContext, ContactConfig.DIRECTORY+"/httpGroupLog", "msg_group", "响应报文  url =" + ServerUrl.MOD_DELETE_PERSONAL_GROUP
						    		+"\r\n xml ="+responseInfo.result, true);	
							
						}

						@Override
						public void onFailure(HttpException error, String msg) {
							// TODO Auto-generated method stub
							ResultModel resultmodel =new ResultModel();
							resultmodel.setRetcode(-8);
							sendMessage(resultmodel);
						}
						
					});
				} else {
					List<Contact> delContacts = new ArrayList<Contact>();
					delContacts.add(getMemberId(ContactConfig.getMobile(mContext),
							groupID));
					String xmlData=Http_Group_Util.get_deleteGroupMemberXML(groupID,JsonParseUtil.contactsIdsTOjson(delContacts),"0");
					Map<String, String> requestMap=new HashMap<String, String>();
					requestMap.put(HttpUtil.XML, xmlData);
					HttpUtil.HttpRequest(mContext, RequestMethod.POST, ServerUrl.DEL_GROUP_MEMBER, false,requestMap, new HttpRequestCallBack<String>(){

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							// TODO Auto-generated method stub
							doResult(responseInfo.result);
							Log.e("退出群组", responseInfo.result);
						}

						@Override
						public void onFailure(HttpException error, String msg) {
							// TODO Auto-generated method stub
							ResultModel resultmodel =new ResultModel();
							resultmodel.setRetcode(-8);
							sendMessage(resultmodel);
						}
						
					});
				}
			}
		});
			
			
//		} catch (SocketTimeoutException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			resultmodel.setRetcode(-9);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			resultmodel.setRetcode(-8);
//			e.printStackTrace();
//		}

		
	}

	/** 获取成员id **/
	private Contact getMemberId(String phoneNumbar, String groupid) {
		String id = dbHelper.getMemberIdInGroup(phoneNumbar, groupid);
		Contact contact = new Contact();
		contact.setIdInGroup(id);
		return contact;
	}
	
	private void sendMessage(ResultModel mResult){
		mHandler.obtainMessage(what, mResult).sendToTarget();
	}
	
	
	private void doResult(String result){
		ResultModel resultmodel = Http_Group_Util.parseResult(result);
		if (resultmodel.getRetcode() == 0
				|| resultmodel.getRetcode() == 3) {
			dbHelper.delGroupById(groupID);
			dbHelper.delMemberByGId(groupID);
			dbHelper.delGroupVersionByGId(groupID);
			Group delgroup = new Group();
			delgroup.setGroup_id(groupID);
//			PushUtils.MsgSyncGroupOrMember(mContext, 0, delgroup, null,null,false);
		}
		ArrayList<Group> sContacts = (ArrayList<Group>) dbHelper.queryAllGroup();
		resultmodel.setResultData(sContacts);
		sendMessage(resultmodel);
	}
	
}
